package com.translate.web.service.impl;

import com.translate.web.config.contract.IOSSConfig;
import com.translate.web.constant.RedisConstant;
import com.translate.web.dao.ActivityManagerMapper;
import com.translate.entity.ActivityManager;
import com.translate.vo.contract.IActivityManagerVO;
import com.translate.web.service.IActivityManagerService;
import com.common.util.CopyEntity;

import java.util.List;
import javax.annotation.Resource;

import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;


@Service
public class ActivityManagerServiceImpl extends BaseTranslateServiceImpl<ActivityManager> 
	implements IActivityManagerService {
	

	private IOSSConfig config;
	
	@Resource
	public void setConfig(IOSSConfig config) {
		this.config = config;
	}


	@Resource
	public void setRedisTemplate(RedisTemplate<String, ? extends Object> redisTemplate) {
		this.redisTemplate = redisTemplate;
	}


	public ActivityManagerServiceImpl(ActivityManagerMapper mapper) {
		super(mapper);
	}
	
	@Override
	public <VO extends IActivityManagerVO> List<VO> getListFromCache(Class<VO> target) {
		return this.entityConvertToVo(this.listToCache(RedisConstant.ACTIVITY_LIST, 1), target);
	}
	
	protected void parseAfter(List<ActivityManager> data) {
		
		String url = "https://".concat(config.getBucketName()).concat(".").concat(config.getEndpoint());
		
		for (ActivityManager banner : data) {
			banner.setPic(url.concat(banner.getPic()));
		}
	}
	

	protected Class<ActivityManager> getEntityClass() {
		return ActivityManager.class;
	}


	@Override
	public <VO extends IActivityManagerVO> VO getArticleById(int id, Class<VO> target) {
		
		ActivityManager articleDetailToCache = this.getArticleDetailToCache(id, redisTemplate,RedisConstant.getActivityId(id));
		
		if (null == articleDetailToCache) {
			return null;
		}
		
		return CopyEntity.copyEntity(articleDetailToCache, target);
	}
}